<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
    <link rel="stylesheet" href="../statics/css/bootstrap.min.css">
    <link rel="stylesheet" href="../statics/HTMLCSS/login.css">
    <meta name="viewport" content="width=device-width,initial-scale=1" />
</head>

<body background="../statics/Photo/Cities.jpg" style="background-repeat: no-repeat;
    background-size: 100% 100%;
    background-attachment: fixed;">
    <div class="container">
        <p id="title">Write life📕</p>
        <div class="col">
            <div class="alert alert-danger alert-dismissible fade show invisible text-center"
                style="width: 34rem;top: 8rem; background-color:white;" role="alert">
                <strong>Tips：</strong><label for="" id="msg">QAQ</label>
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form id="loginForm" action="/loginDone" method="POST">
                <p id="number">Number:&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="username" id="username"
                        style="border-top: none;border-left: none;border-right: none; border-bottom:solid 1px;width:300px;height: 30px;font-size: 20px;">
                </p>
                <p id="password">Password:&nbsp;&nbsp;&nbsp;&nbsp;<input type="password" name="passwords"
                        placeholder="8-16code" maxlength="16" id="passwords"
                        style="border-top: none;border-left: none;border-right: none; border-bottom:solid 1px;width:300px;height: 30px;font-size: 20px;">
                </p>

                <label><img src="../statics/Photo/闭眼.png" id="eyes" alt=""></label>
                <label><img src="../statics/Photo/睁眼.png" id="openEyes" class="invisible"></label>

                <p id="login"><input type="button" value="Login" onclick="Login()" style="height:60px; width:150px;">
                </p>
                <p id="registe"><input type="button" value="Registe" onclick="Registe()"
                        style="height:60px; width:150px;">
                </p>
            </form>

        </div>

        <script src="../statics/js/jquery-3.6.0.min.js"></script>
        <script src="../statics/js/bootstrap.min.js"></script>

        <script>
            var input = document.querySelector('input');
            var imgs = document.getElementById('eyes');
            var img = document.getElementById('openEyes');
            var flag = 0;
            imgs.onclick = function () {
                if (flag == 0) {
                    input.type = 'text';
                    openEyes.src = '../statics/Photo/睁眼.png';
                    flag = 1;
                } else {
                    input.type = 'password';
                    eyes.src = '../statics/Photo/闭眼.png';
                    $('#openEyes').removeClass('invisible');
                    flag = 1;
                }
            }
            function Login() {
                let username = $('#username').val();
                let password = $('#passwords').val();
                if (username.length > 0 && password.length > 0) {
                    // ajax请求
                    $.ajax({
                        url: "/loginDone",
                        type: "POST",
                        data: $('#loginForm').serialize(),                      
                        dataType: "json",                      
                        success: function (res) {
                            console.log(res);
                            if (res.code === 200) {
                                window.location.href = "/";
                            } else {
                                alert_fn(res.msg);
                            }

                        },
                        error: function (msg) {
                            alert_fn('There is a problem with the network or server');
                        }
                       
                    })
                } else {// 验证不通过，提示相关信息，并且阻止提交
                    alert_fn('Incorrect user name or password!');
                    return false;
                }
            }

            // 这个方法包装了alert的相关行为和显示的内容
            function alert_fn(msg) {
                msg = msg || '密码不能为空，两次密码应该一致';
                // 替换提示的内容
                $('#msg').text(msg);
                // 移除不显示的class，则alert将显示
                $('.invisible').removeClass('invisible');
                // 三秒中后，给alert增加不显示的class，则alert重新隐藏
                setTimeout(() => {
                    $('.alert').addClass('invisible');
                }, 5000);
            }

            function Registe() {
                window.location.href = '/register';
            }
        </script>
</body>

</html>